package com.want.buss.interfaces.service.impl;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.want.buss.interfaces.dto.CustomerStockParam;
import com.want.buss.interfaces.mapper.CustomerStockMapper;
import com.want.buss.interfaces.po.CustomerStock;
import com.want.buss.interfaces.po.CustomerStockData;
import com.want.buss.interfaces.service.CustomerStockService;
import com.want.buss.interfaces.utils.BussStringUtils;

//@Service //junit 测试使用
@WebService
public class CustomerStockServiceImpl implements CustomerStockService {	
	Logger logger = Logger.getLogger(CustomerStockServiceImpl.class);  
	@Autowired
	CustomerStockMapper customerStockMapper;
	
	@WebMethod
	@Override
	public CustomerStockData queryCustomerStock(@WebParam(name = "customerStockParam") CustomerStockParam customerStockParam) {
		CustomerStockData data = new CustomerStockData(); 
		// 参数判断
		if (validateParam(customerStockParam)!="") {
			data.setSuccess(false);
			data.setMsg(validateParam(customerStockParam));
		}else{
			try {
				List<CustomerStock> customerStocks=customerStockMapper.queryCustomerStock(customerStockParam);
				//结果验证
				if(customerStocks!=null && customerStocks.size()>0 && customerStocks.get(0).getProdYmCqty() == null){
					if(customerStocks.get(0).getProdYmCqty() == null){
						data.setSuccess(true);
						data.setMsg("客户尚无该品项最新的库存盘点数据");
					}
				}else{
					data.setSuccess(true);
					data.setCustomerStocks(customerStocks);
				}
			} catch (Exception e) {
				data.setSuccess(false);
				data.setMsg(e.getMessage());
			}
		}
		return data;
	}
	
	/**
	 * 验证接口接收参数是否合理
	 * @param   CustomerStockParam
	 * @return  List<String>
	 */
	private String validateParam(CustomerStockParam param) {
		String msg = "";
		
		if(param!=null){
			if(BussStringUtils.isEmpty(param.getCustomerId())&&
					BussStringUtils.isEmpty(param.getCustomerName())){
				msg+="客户编码和客户名称必须输入一个值;\n";
			}
			
			if(BussStringUtils.isEmpty(param.getProdId())&&
					BussStringUtils.isEmpty(param.getProdName())){
				msg+="品项编码和品项名称必须输入一个值;\n";
			}
			
			if(BussStringUtils.isEmpty(param.getProdYearMonth())){
				msg+="货龄年月不能为空;\n";
			}
		}else{
			msg+="传入参数不能为空;\n";
		}
		return msg;
	}
	
}
